import time
from machine import Pin, PWM, I2C
from rocker import ROCKER
from ssd1306 import SSD1306_I2C
rocker = ROCKER()  # 初始化摇杆
scr = SSD1306_I2C(128, 64, I2C(0, sda=Pin(4), scl=Pin(5)))
scr.fill(0)
scr.show()
pwm = PWM(Pin(21))
pwm.freq(50)
DIR_0 = 500000  # 0度
DIR_45 = 1000000  # 45度
DIR_90 = 1500000  # 90度
DIR_135 = 2000000  # 135度
DIR_180 = 2500000  # 180度
# pwm.duty_ns(DIR_0)
# time.sleep_ms(400)
# pwm.duty_ns(DIR_90)
# time.sleep_ms(400)
# pwm.duty_ns(DIR_45)
# time.sleep_ms(400)
# pwm.duty_ns(DIR_0)
# time.sleep_ms(400)
# pwm.duty_ns(DIR_90)
# time.sleep_ms(400)
# pwm.duty_ns(DIR_180)
# time.sleep_ms(400)
# pwm.duty_ns(DIR_45)
# time.sleep_ms(400)
# pwm.duty_ns(DIR_90)
# time.sleep_ms(400)
# pwm.deinit()
# for i in range(500000, 2500000, 50000):
#      pwm.duty_ns(i)
#      print(i)
#      time.sleep_ms(30)
pwm.duty_ns(DIR_0)
time.sleep_ms(400)
scr = SSD1306_I2C(128, 64, I2C(0, sda=Pin(4), scl=Pin(5)))
scr.fill(0)
scr.show()
for i in range(0, 10000):
    scr.fill(0)
    scr.text("X : " + rocker.getX() + " %", 0, 12)
    pwm.duty_ns(25000*int(float(rocker.getX())))
    scr.text("v:" + str(25000*int(float(rocker.getX()))), 0, 24)
    scr.show()
    time.sleep_ms(50)
pwm.deinit()
